class NewslettersController < ApplicationController
  
  before_filter :load_newsletter, :except => [:index, :new, :create]

  def index
    @newsletters = Newsletter.find(:all)
  end
  
  def show
  end
  
  def new
    @newsletter = Newsletter.new
  end
  
  def create
    @newsletter = Newsletter.new(params[:newsletter])
    if(@newsletter.save)
      flash[:success] = "Newsletter successfuly created."
      redirect_to(newsletters_path)
    else
      flash[:error] = "Error creating newsletter."      
      render(:action => :new)
    end    
  end
  
  def edit
  end
  
  def update
    if @newsletter.update_attributes(params[:newsletter])
      flash[:success] = "Newsletter successfuly updated."
      redirect_to(newsletters_path)
    else
      render(:action => :edit)
    end    
  end

  def activate
    @newsletter = Newsletter.find(params[:id])
    if @newsletter.activate!
      flash[:success] = "Newsletter successfuly activated !"
    else
      flash[:error] = "Error activating newsletter !"
    end
    redirect_to(newsletters_path)
  end
  
  def destroy
    @newsletter.destroy
    flash[:success] = "Newsletter successfuly deleted !"
    redirect_to(newsletters_path)
  end

  private

  def load_newsletter
    @newsletter = Newsletter.find(params[:id])
   rescue ActiveRecord::RecordNotFound
    flash[:error] = "Newsletter not found."
    redirect_to(newsletters_path)
  end
  
end
